<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <style>
        * {
            list-style: none;
            margin: 0;
            padding: 0;
        }

        #shop-car li {
            border: 1px dotted red;
            margin: 20px 0;
            padding: 20px;
        }

        .container {
            width: 400px;
            padding: 20px;
        }
    </style>

    <div class="container">
        


        <ul id="shop-car">
            <!-- <li>
                <input type="checkbox" />
                <div>商品名称:</div>
                <img src="https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg" alt="">
                <div>商品价格:50</div>
                <div>购买数量:30</div>
                <div>商品规格: 黑</div>
                <div>是否选中:不选中</div>
            </li>
            <li>
                <input type="checkbox" />
                <div>商品名称:</div>
                <img src="https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg" alt="">
                <div>商品价格:50</div>
                <div>购买数量:30</div>
                <div>商品规格: 黑</div>
                <div>是否选中:不选中</div>
            </li> -->
        </ul>
        <p id="totalnum">总数量:0</p>
        
    </div>
<script>
    let products = [
        {
            goods_name: '小米10-1',
            goods_img: 'https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg',
            goods_price: '500',
            goods_attr: '砂石黑4GB+64GB',
            goods_num: 10,
            goods_is_checked: true
        },
        {
            goods_name: '小米10-2',
            goods_img: 'https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg',
            goods_price: '510',
            goods_attr: '砂石黑4GB+64GB',
            goods_num: 50,
            goods_is_checked: false
        }
]
</script>
       
    
    
</body>
<script src="./jquery-3.6.0.min.js"></script>
<script>
    var int = document.getElementsByTagName('input');
    var odiv = document.getElementsByTagName('div');
    var zl = document.getElementsByTagName('p')[0];
    var nr=document.getElementById('shop-car');
var str='';
var num=0;
    for(var i=0;i<products.length;i++){
        str+=`
            <li>
                <input type="checkbox" ${products[i].goods_is_checked ? "checked" :''}/>
                <div>商品名称:${products[i].goods_name}</div>
                <img src="${products[i].goods_img}" alt="">
                <div>商品价格:${products[i].goods_price}</div>
                <div>购买数量:${products[i].goods_num}</div>
                <div>商品规格:${products[i].goods_attr}</div>
                <div>是否选中:${products[i].goods_is_checked ? "选中吧" :"不选中"}</div>
            </li> 
    `
            products[i].goods_is_checked ? num+=products[i].goods_num :''
    }
    $('p').html(`总数量:${num}`);
    nr.innerHTML=str;
    // int[0].onclick=function(){      
    //     if(int[0].checked){
    //         odiv[5].innerHTML="是否选中:选中吧";
            
    //     }else{
    //         odiv[5].innerHTML="是否选中:不选中";
           
    //     }
    // }
    // int[1].onclick=function(){
    //     if(int[1].checked){
    //         odiv[10].innerHTML="是否选中:选中吧";
           

    //     }else{
    //         odiv[10].innerHTML="是否选中:不选中";
         
    //     }
    // }

</script>
<script>
    $('input').click(function(){
        $(this).parent().find('div:last-of-type').html($(this).prop('checked') ? '是否选中:选中吧':'是否选中:不选中');
        var arr=$(':checkbox:checked').parent().find('div:nth-of-type(3)').text().split('购买数量:');
             var s=0;
             for(var i=0;i<arr.length;i++){
                s+=Number(arr[i]);
             }
        $('p').html(`总数量:${s}`);
       
        
    });
</script>
</html>